<template>
  <div class="mod-user">
    <el-form :inline="true" :model="formData" label-width="100px" label-position="right">
      <el-form-item label="合伙人名称" v-if="false">
        <el-input @change="searchChange" placeholder="合伙人名称" v-model="formData.partnerName" size="small"
          clearable></el-input>
      </el-form-item>
      <el-form-item label="合伙人手机">
        <el-input @change="searchChange" placeholder="手机号" v-model="formData.partnerMobile" size="small"
          clearable></el-input>
      </el-form-item>
      <el-form-item label="账单时间">
        <el-input @change="searchChange" placeholder="账单时间(示例:20240231)" v-model="formData.billTime" size="small"
          clearable></el-input>
      </el-form-item>
      <el-form-item label="合伙人角色" v-if="false">
        <el-select size="small" v-model="formData.partnerRole" @change="searchChange" clearable>
          <el-option v-for="(item, index) in partnerRoleList" :key="index" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="加盟日期" v-if="false">
        <el-date-picker v-model="formData.createDate" @change="searchChange" size="small" value-format="yyyy-MM-dd"
          type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.stop="addOrUpdate()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.stop="searchChange">搜索</el-button>
      </el-form-item>
    </el-form>
    <avue-crud ref="crud" :page="page" :data="dataList" :option="tableOption" @size-change="sizeChange"
      @current-change="currentChange">
      <template class="billContent" slot-scope="scope" slot="billContent">
        <div style="display: flex; flex-direction: column; align-items: center;">
          <div v-if="scope.row.billType == 0">费用：<span>{{ scope.row.partnerDeviceBillParamVo.money }}元</span></div>
          <div v-if="scope.row.billType == 0">账期：<span>{{ scope.row.partnerDeviceBillParamVo.timeType == 0 ? "日" :
            "月" }}</span></div>
        </div>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
        <el-tag v-else size="small">正常</el-tag>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button type="primary" icon="el-icon-edit" size="small" @click.stop="addOrUpdate(scope.row.id)">编辑</el-button>
        <el-button type="primary" size="small" @click.stop="deleteBill(scope.row.id)">删除</el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { tableOption } from '@/crud/partner/bill'
export default {
  data() {
    return {
      dataList: [],
      dataListLoading: false,
      tableOption: tableOption,
      formData: {
        partnerName: '',
        partnerMobile: ''
      },
      partnerDeviceBillParamVo: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      options: [],
      props: {
        multiple: false,
        label: 'name',
        value: 'id',
        children: 'childs'
      },
    }
  },

  mounted() {
    this.getDataList()
  },

  methods: {
    // 获取数据列表
    getDataList(page, params) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/v1/partner/bill/list'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            params
          )
        )
      }).then(({ data }) => {
        this.dataList = data.data.records
        this.page.total = data.data.total
        this.dataListLoading = false
      })
    },

    // 条件查询
    searchChange() {
      this.getDataList(this.page, this.formData)
    },

    addOrUpdate(id) {
      console.log("id是", id)
      this.$router.push({
        name: 'addPartnerBill',
        query: {
          id: id,
          type: 2
        }
      })
    },

    deleteBill(id) {
      this.$http({
        url: this.$http.adornUrl('/v1/partner/bill/delete'),
        method: 'post',
        data: this.$http.adornData(
          Object.assign(
            {
              id: id
            }
          )
        )
      }).then(({ data }) => {
        if (data.code === 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        } else {
          this.$message({
            message: '删除失败',
            type: 'error'
          }).catch(error => {
            console.error(error);
            this.$message({
              message: '请求失败',
              type: 'error'
            });
          });;
        }
        this.getDataList(this.page, this.formData)
      })
    },
    sizeChange(val) {
      this.page.pageSize = val
      this.getDataList(this.page, this.formData)
    },
    currentChange(val) {
      this.page.currentPage = val
      this.getDataList(this.page, this.formData)
    }
  }
}
</script>

<style lang="scss">
/*.cell{*/
/*text-align: left;*/
/*}*/
.el-input {
  width: 200px;
}
</style>
